package cn.jinjimi.app.store.entity;

import cn.jinjimi.base.util.DateUtils;
import cn.jinjimi.base.util.GsonUtils;
import cn.jinjimi.base.util.MD5Utils;
import cn.jinjimi.base.util.StringUtils;
import cn.jinjimi.app.AccountManager;
import cn.jinjimi.app.BuildConfig;
import cn.jinjimi.app.controller.event.NoticeEvent.NoticeRewardCardEvent;
import cn.jinjimi.domain.data.dto.NoticeMeMenuDto;
import cn.jinjimi.domain.data.dto.NoticeNewVersionDTO;
import cn.jinjimi.domain.data.dto.NoticePosterJinjuDTO;
import cn.jinjimi.app.entity.ActivityInfo;
import cn.jinjimi.app.entity.NotificationContent;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * Created by Roy
 * Date: 16/11/21
 */

public class NoticeEntity {
    public Long id;
    public String noticeId;
    public String code;
    public String content;
    public Long startTime;
    public Long expirTime;
    public String ssoid;
    public Boolean hadShowed;

    // KEEP FIELDS - put your custom fields here

    public final static String TypeActivityInfo = "activity_info";//  展业计划红点
    public final static String TypeNewVersion = "new_version";// 新版本
    public final static String TypeNewFeature = "me_new_feature";//我的新特性
    public final static String TypeBigDay = "big_day";//  客户节日提醒
    public final static String TypeNewClue = "new_clue";// 展业获客
    public final static String TypeDrpEvent = "drp_event";// 赚多保喜报
    public final static String TypeNewPoster = "new_poster";//海报
    public final static String TypeNewJinju = "new_logion";//金句
    public final static String TypeAlert = "alert"; //AppAlert for content=[crm,drp,me]
    public final static String TypeNewArticle = "new_article";// 好文红点
    public static final String TypeRewardCard = "new_coupon";//首页奖券卡

    // KEEP FIELDS END

    public NoticeEntity() {
    }

    public NoticeEntity(Long id) {
        this.id = id;
    }

    public NoticeEntity(String noticeId, String code, String content, Long expirTime, String ssoid) {
        this.noticeId = noticeId;
        this.code = code;
        this.content = content;
        this.expirTime = expirTime;
        this.ssoid = ssoid;
        this.hadShowed = false;
    }

    public NoticeEntity(Long id, String noticeId, String code, String content, Long startTime, Long expirTime, String ssoid, Boolean hadShowed) {
        this.id = id;
        this.noticeId = noticeId;
        this.code = code;
        this.content = content;
        this.startTime = startTime;
        this.expirTime = expirTime;
        this.ssoid = ssoid;
        this.hadShowed = hadShowed;
    }

    // KEEP METHODS - put your custom methods here

    public static String[] createAllCodes() {
        return new String[]{TypeActivityInfo, TypeNewVersion, TypeNewFeature, TypeBigDay, TypeNewClue, TypeDrpEvent, TypeNewPoster, TypeNewJinju, TypeAlert, TypeNewArticle, TypeRewardCard};
    }

    /**
     * 根据通知系统返回结果创建红点数据
     *
     * @param notificationContents
     * @return
     */
    public static List<NoticeEntity> createNoticeEntities(NotificationContent... notificationContents) {
        List<NoticeEntity> noticeEntities = new ArrayList<>();

        for (NotificationContent nc : notificationContents) {
            if (StringUtils.isEmpty(nc.code))
                continue;

            if (nc.code.equals(TypeNewVersion)) {// 判断newVersion红点通知下versionCode字段是否小于本地versionCode版本
                NoticeNewVersionDTO dtoVersion = GsonUtils.fromJson(nc.content, NoticeNewVersionDTO.class);
                if (dtoVersion == null || dtoVersion.versionCode <= BuildConfig.VERSION_CODE)
                    continue;
            }

            if (nc.code.equals(TypeNewFeature)) {// 判断newFrature情况下name字段是为空的情况
                NoticeMeMenuDto dtoMeMenu = GsonUtils.fromJson(nc.content, NoticeMeMenuDto.class);
                if (dtoMeMenu == null || StringUtils.isEmpty(dtoMeMenu.title))
                    continue;
            }

            NoticeEntity noticeEntity = new NoticeEntity();
            noticeEntity.noticeId = nc.id;
            noticeEntity.code = nc.code;
            noticeEntity.content = nc.content;
            noticeEntity.hadShowed = false;

            if (nc.tag != null && StringUtils.isNotEmpty(nc.tag.ssoId))
                noticeEntity.ssoid = nc.tag.ssoId;

            noticeEntity.startTime = nc.created_time;

            if (StringUtils.isEmpty(nc.expire_time)) {// 过期时间为null的情况，默认为creatTime算起3个月内（自己定义的）
                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(nc.created_time);
                calendar.add(Calendar.MONTH, 3);
                noticeEntity.expirTime = calendar.getTimeInMillis();
            } else {
                noticeEntity.expirTime = Long.valueOf(nc.expire_time);
            }

            noticeEntities.add(noticeEntity);
        }

        return noticeEntities;
    }

    /**
     * 更新NoticeEntity
     *
     * @param nc
     * @return
     */
    public NoticeEntity updateByNc(NotificationContent nc) {
        if (StringUtils.isNotEmpty(nc.code))
            this.code = nc.code;

        if (this.code.equals(TypeNewVersion)) {
            NoticeNewVersionDTO dtoVersion = GsonUtils.fromJson(nc.content, NoticeNewVersionDTO.class);
            if (dtoVersion != null && dtoVersion.versionCode > BuildConfig.VERSION_CODE)
                this.content = nc.content;
        } else if (this.code.equals(TypeNewFeature)) {
            NoticeMeMenuDto dtoMeMenu = GsonUtils.fromJson(nc.content, NoticeMeMenuDto.class);
            if (dtoMeMenu != null && StringUtils.isNotEmpty(dtoMeMenu.title))
                this.content = nc.content;
        } else {
            this.content = nc.content;
        }

        if (nc.tag != null && StringUtils.isNotEmpty(nc.tag.ssoId))
            this.ssoid = nc.tag.ssoId;

        this.startTime = nc.created_time;
        if (StringUtils.isEmpty(nc.expire_time)) {// 过期时间为null的情况，默认为creatTime算起3个月内（自己定义的）
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(nc.created_time);
            calendar.add(Calendar.MONTH, 3);
            this.expirTime = calendar.getTimeInMillis();
        } else {
            this.expirTime = Long.valueOf(nc.expire_time);
        }

        return this;
    }

    /**
     * 根据展业详情创建红点数据
     *
     * @param activityInfo
     * @return
     */
    public static List<NoticeEntity> createNoticeEntities(ActivityInfo activityInfo) {
        List<NoticeEntity> noticeEntities = new ArrayList<>();
        if (activityInfo == null || activityInfo.data == null || activityInfo.data.alerts == null)
            return Collections.emptyList();

        for (ActivityInfo.Alert alert : activityInfo.data.alerts) {
            NoticeEntity noticeEntity = new NoticeEntity();

            noticeEntity.noticeId = MD5Utils.MD5(activityInfo.link + AccountManager.getInstance().getAccount().ssoid);
            noticeEntity.code = TypeActivityInfo;
            noticeEntity.hadShowed = false;

            Date startDate = DateUtils.ParseHHmmssString(alert.time);
            Date endDate = DateUtils.ParseHHmmssString(alert.endTime);
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());

            if (startDate == null) {
                continue;
            }

            if (startDate != null) {
                Calendar calendar1 = Calendar.getInstance();
                calendar1.setTime(startDate);
                calendar.set(Calendar.HOUR_OF_DAY, calendar1.get(Calendar.HOUR_OF_DAY));
                calendar.set(Calendar.MINUTE, calendar1.get(Calendar.MINUTE));
                calendar.set(Calendar.SECOND, calendar1.get(Calendar.SECOND));
                calendar.set(Calendar.MILLISECOND, 0);
            }

            noticeEntity.startTime = calendar.getTimeInMillis();

            if (endDate != null) {
                Calendar calendar1 = Calendar.getInstance();
                calendar1.setTime(endDate);
                calendar.set(Calendar.HOUR_OF_DAY, calendar1.get(Calendar.HOUR_OF_DAY));
                calendar.set(Calendar.MINUTE, calendar1.get(Calendar.MINUTE));
                calendar.set(Calendar.SECOND, calendar1.get(Calendar.SECOND));
                calendar.set(Calendar.MILLISECOND, 0);
            } else {
                calendar.add(Calendar.HOUR_OF_DAY, 2);//默认两小时后失效
            }
            noticeEntity.expirTime = calendar.getTimeInMillis();

            noticeEntities.add(noticeEntity);
        }
        return noticeEntities;
    }

    /* 根据NoticeEntity转化成喜报显示的字符串 */
    public static List<String> getDrpNews(List<NoticeEntity> noticeEntities) {
        if (!noticeEntities.isEmpty()) {
            List<String> news = new ArrayList<>();
            for (NoticeEntity noticeEntity : noticeEntities) {
                news.add(noticeEntity.content);
            }
            return news;
        }
        return null;
    }

    /* 根据NoticeEntity转化成海报 or 金句 tab的红点 */
    public static List<NoticePosterJinjuDTO> getNewPosterInforms(List<NoticeEntity> noticeEntities) {
        List<NoticePosterJinjuDTO> mList = new ArrayList<>();
        if (noticeEntities != null && !noticeEntities.isEmpty()) {

            for (NoticeEntity noticeEntity : noticeEntities) {
                if (noticeEntity != null) {
                    String content = noticeEntity.content;
                    if (StringUtils.isNotEmpty(content)) {
                        NoticePosterJinjuDTO data = GsonUtils.fromJson(content, NoticePosterJinjuDTO.class);
                        data.noticeId = noticeEntity.noticeId;
                        mList.add(data);
                    }
                }
            }

        }
        return mList;
    }

    /* 根据NoiceEntity转化成我界面的红点信息 */
    public static List<NoticeMeMenuDto> getMeNotices(List<NoticeEntity> noticeEntities) {
        List<NoticeMeMenuDto> datas = new ArrayList<>();

        for (NoticeEntity noticeEntity : noticeEntities) {
            if (StringUtils.isNotEmpty(noticeEntity.content)) {
                NoticeMeMenuDto data = GsonUtils.fromJson(noticeEntity.content, NoticeMeMenuDto.class);
                if (data != null && StringUtils.isNotEmpty(data.title)) {
                    data.noticeId = noticeEntity.noticeId;
                    datas.add(data);
                }
            }
        }

        return datas;
    }

    public static List<NoticeRewardCardEvent.Coupon> getCoupons(List<NoticeEntity> noticeEntities) {
        List<NoticeRewardCardEvent.Coupon> coupons = new ArrayList<>();
        for (NoticeEntity noticeEntity : noticeEntities) {
            if (StringUtils.isNotEmpty(noticeEntity.content)) {
                NoticeRewardCardEvent.Coupon coupon = GsonUtils.fromJson(noticeEntity.content, NoticeRewardCardEvent.Coupon.class);
                if (coupon != null && StringUtils.isNotEmpty(coupon.imgUrl) && StringUtils.isNotEmpty(coupon.linkUrl)) {
                    coupons.add(coupon);
                }else{
                    coupons.add(null);
                }
            }
        }
        return coupons;
    }

    // KEEP METHODS END
}
